package ar.edu.utn.frba.gdaia.src.swing.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

public class GdaiaGuiFrame extends JFrame {
    private JRadioButton BtnDespierto = new JRadioButton();
    private JRadioButton BtnDormido = new JRadioButton();
    private JLabel jLabel1 = new JLabel();
    private JTextField Txtteclado = new JTextField();
    private JLabel jLabel2 = new JLabel();
    private JTextField Txtvideo = new JTextField();
    private JLabel jLabel3 = new JLabel();
    private JTextField Txtaudio = new JTextField();
    private JLabel jLabel4 = new JLabel();
    private JTextField Txtimagen = new JTextField();
    private JCheckBox ChkTexto = new JCheckBox();
    private JCheckBox ChkVideo = new JCheckBox();
    private JCheckBox ChkAudio = new JCheckBox();
    private JCheckBox ChkImagen = new JCheckBox();
    private JTextArea salida = new JTextArea();
    private JScrollPane jScrollPane1 = new JScrollPane();
    private JTextArea textareaEstado = new JTextArea();
    private JLabel theImage=new JLabel("");


    // Constantes
    private static final String TIP_INGRESE_COMANDO = "Ingrese Comando...";
    private static final String TIP_ESTADO_SISTEMA = "Estado del sistema";
    private static final String TITULO_VENTANA = "GDAIA v1.0  UTN-FRBA";
    private static final String DESCRIPCION_ESTADO = "Estado del Sistema:";
    private static final String ESTADO_DESPIERTO = "DESPIERTO";
    private static final String ESTADO_DORMIDO = "DORMIDO";
    private JButton Ingresar = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JButton jButton4 = new JButton();
    private JPanel imagen1 = new JPanel();

    public GdaiaGuiFrame() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(585, 456));
        this.setTitle(TITULO_VENTANA);
        this.setResizable(false);
        BtnDespierto.setText("DESPIERTO");
        BtnDespierto.setBounds(new Rectangle(140, 370, 110, 25));
        BtnDespierto.setSelected(true);
        BtnDespierto.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        BtnDespierto_actionPerformed(e);
                    }
                });
        BtnDormido.setText("DORMIDO");
        BtnDormido.setBounds(new Rectangle(10, 370, 110, 25));
        BtnDormido.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        BtnDormido_actionPerformed(e);
                    }
                });
        jLabel1.setText("Entrada por teclado");
        jLabel1.setBounds(new Rectangle(425, 15, 115, 25));
        Txtteclado.setBounds(new Rectangle(405, 40, 155, 35));
        Txtteclado.setAutoscrolls(false);
        jLabel2.setText("Entrada por Video");
        jLabel2.setBounds(new Rectangle(430, 85, 105, 20));
        Txtvideo.setBounds(new Rectangle(405, 105, 120, 20));
        Txtvideo.setEditable(false);
        jLabel3.setText("Entrada por audio");
        jLabel3.setBounds(new Rectangle(430, 145, 105, 20));
        Txtaudio.setBounds(new Rectangle(405, 170, 120, 20));
        Txtaudio.setEditable(false);
        jLabel4.setText("Entrada por Imagen");
        jLabel4.setBounds(new Rectangle(430, 210, 110, 15));
        Txtimagen.setBounds(new Rectangle(405, 235, 120, 20));
        Txtimagen.setEditable(false);
        this.getContentPane().add(imagen1, null);
        this.getContentPane().add(jButton4, null);
        this.getContentPane().add(jButton3, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(Ingresar, null);
        this.getContentPane().add(textareaEstado, null);
        jScrollPane1.getViewport().add(salida, null);
        this.getContentPane().add(jScrollPane1, null);
        this.getContentPane().add(ChkImagen, null);
        this.getContentPane().add(ChkAudio, null);
        this.getContentPane().add(ChkVideo, null);
        this.getContentPane().add(ChkTexto, null);
        this.getContentPane().add(Txtimagen, null);
        this.getContentPane().add(jLabel4, null);
        this.getContentPane().add(Txtaudio, null);
        this.getContentPane().add(jLabel3, null);
        this.getContentPane().add(Txtvideo, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(Txtteclado, null);
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(BtnDormido, null);
        this.getContentPane().add(BtnDespierto, null);
        ChkTexto.setBounds(new Rectangle(400, 15, 20, 20));
        ChkTexto.setSelected(true);
        ChkTexto.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ChkTexto_actionPerformed(e);
                    }
                });
        ChkVideo.setBounds(new Rectangle(400, 85, 20, 20));
        ChkVideo.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ChkVideo_actionPerformed(e);
                    }
                });
        ChkAudio.setBounds(new Rectangle(400, 145, 20, 20));
        ChkAudio.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ChkAudio_actionPerformed(e);
                    }
                });
        ChkImagen.setBounds(new Rectangle(400, 210, 20, 20));
        ChkImagen.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ChkImagen_actionPerformed(e);
                    }
                });
        jScrollPane1.setBounds(new Rectangle(15, 10, 365, 345));
        textareaEstado.setBounds(new Rectangle(0, 405, 580, 25));
        textareaEstado.setEditable(false);
        textareaEstado.setBackground(Color.LIGHT_GRAY);

        // Barra de estado
        textareaEstado.setText(DESCRIPCION_ESTADO + " " + ESTADO_DESPIERTO + 
                               "\n");
        Ingresar.setText("Ingresar");
        Ingresar.setBounds(new Rectangle(265, 370, 115, 25));
        Ingresar.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        Ingresar_actionPerformed(e);
                    }
                });
        jButton2.setText("...");
        jButton2.setBounds(new Rectangle(530, 105, 35, 20));
        jButton3.setText("...");
        jButton3.setBounds(new Rectangle(530, 170, 35, 20));
        jButton4.setText("...");
        jButton4.setBounds(new Rectangle(530, 235, 35, 20));
        jButton4.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jButton4_actionPerformed(e);
                    }
                });
        imagen1.setBounds(new Rectangle(390, 265, 180, 125));

        // Create a panel to hold the image
        JPanel imagePanel = new JPanel();
        imagePanel.setLayout(new BorderLayout());
        imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        JScrollPane scroller = new JScrollPane(theImage);
        scroller.getVerticalScrollBar().setUnitIncrement(10);
        scroller.getHorizontalScrollBar().setUnitIncrement(10);
        imagePanel.add(scroller, BorderLayout.CENTER);
        imagen1.setLayout(new BorderLayout()); 
        imagen1.add(imagePanel,BorderLayout.CENTER);

    }

    private void BtnDespierto_actionPerformed(ActionEvent e) {
        boolean selected = BtnDormido.isSelected();

        if (selected = true) {
            BtnDespierto.setSelected(true);
            BtnDormido.setSelected(false);
        }
    }

    private void BtnDormido_actionPerformed(ActionEvent e) {
        boolean selected = BtnDespierto.isSelected();

        if (selected = true) {
            BtnDormido.setSelected(true);
            BtnDespierto.setSelected(false);
        }
    }

    private void ChkTexto_actionPerformed(ActionEvent e) {

        if (ChkTexto.isSelected() == true) {
            Txtteclado.setEnabled(true);
            Txtteclado.setEditable(true);
        } else {
            Txtteclado.setEnabled(false);
            Txtteclado.setEditable(false);
        }

    }

    private void BtnIngresarteclado_actionPerformed(ActionEvent e) {

    }

    private void ChkVideo_actionPerformed(ActionEvent e) {
        if (ChkVideo.isSelected() == true) {
            JOptionPane.showMessageDialog(null, "Funcion no implementada");
        }
    }

    private void ChkAudio_actionPerformed(ActionEvent e) {
        if (ChkAudio.isSelected() == true) {
            JOptionPane.showMessageDialog(null, "Funcion no implementada");
        }
    }

    private void ChkImagen_actionPerformed(ActionEvent e) {
        if (ChkImagen.isSelected() == false) {
            jButton4.setEnabled(false);
            Txtimagen.setEditable(false);
        } else
            jButton4.setEnabled(true);
        Txtimagen.setEditable(true);
    }

    //TODO: Darle funcionalidad a la pantalla
    private void Ingresar_actionPerformed(ActionEvent e) {
        if (ChkTexto.isSelected() == true) {
            salida.append(Txtteclado.getText() + "\n");
            Txtteclado.setText("");
        }
        if (ChkAudio.isSelected() == true)
            JOptionPane.showMessageDialog(null, 
                                          "Audio: Funcion no implementada");
        if (ChkImagen.isSelected() == true)
            ;//
        if (ChkVideo.isSelected() == true)
            JOptionPane.showMessageDialog(null, 
                                          "Video: Funcion no implementada");
        if (ChkAudio.isSelected() == false && 
            ChkImagen.isSelected() == false && 
            ChkVideo.isSelected() == false && ChkTexto.isSelected() == false)
            JOptionPane.showMessageDialog(null, 
                                          "Por lo menos algun metodo de entrada debe estar seleccionado");
    }

    // Adicion de soporte para leer imagenes JPG y GIF

    private void jButton4_actionPerformed(ActionEvent e) {
        JFileChooser archivo = new JFileChooser();
        File path;
 
        archivo.addChoosableFileFilter(new ImageFilter());
        archivo.showOpenDialog(GdaiaGuiFrame.this);
        path = archivo.getSelectedFile();
        Txtimagen.setText(path.toString());


        theImage.setIcon(new ImageIcon(Txtimagen.getText()));
   }

}

